<?php
namespace app\common\model\orderhawa;

use app\common\model\orderdata\Goods;
use think\Model;

class Hawa extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'hawaorder';
    protected $pk    = 'HawaId';

    const STATEOK  = 0;
    const STATEINT = 1;
    public function goodInfo()
    {
        return $this->hasOne('app\common\model\orderdata\Goods', 'GoodId', 'GoodId');
    }
    public function listinfo()
    {
        return $this->hasOne('HawaList', 'HawaListId', 'HawaListId');
    }
    /**
     * 库存区域信息
     * @return [type] [description]
     */
     public function areainfo()
    {
        return $this->hasOne('app\common\model\orderdata\StorageArea', 'StorageAreaId', 'StorageAreaId');
    }

    /**
     * 获取危废订单
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getlist($data, $type = 'hawa')
    {
        $ndata = Hawa::where('HawaListId', $data['HawaListId'])->with('goodInfo')->select();
        if ($type == 'hawa') {
            $ndata->hidden([ 'good_info.OperationLog', 'good_info.TotalWeight'])->toArray();
        } else if ($type == 'buss') {
            if (BUSSSHOWHAWAQUOT) {
                $ndata->hidden([ 'good_info.OperationLog', 'good_info.TotalWeight'])->toArray();
            } else {
                $ndata->hidden([ 'Price', 'FloatingPrice', 'FloatingNote', 'good_info.OperationLog', 'good_info.TotalWeight'])->toArray();
            }
        } else if ($type == 'acco') {
            $ndata->hidden([ 'good_info.OperationLog', 'good_info.TotalWeight'])->toArray();
        } else if ($type == 'mana') {
            $ndata->hidden([ 'good_info.OperationLog', 'good_info.TotalWeight'])->toArray();
        } else if ($type == 'tran') {
             //运维单位收集员是否能查看并操作价格
            if(!TRAN_OPERATION_HAWA_PRICE){
                $ndata->hidden([ 'good_info.OperationLog', 'good_info.TotalWeight'])->toArray();
            }else{
                $ndata->hidden([ 'Price', 'FloatingPrice', 'FloatingNote', 'good_info.OperationLog', 'good_info.TotalWeight'])->toArray();
            }

        } else if ($type == 'print') {
            $ndata->hidden([ 'Price', 'FloatingPrice', 'FloatingNote', 'good_info.OperationLog', 'good_info.TotalWeight'])->toArray();
        }
        return $ndata;

    }

    /**
     * 增加订单
     * @param  [type] $HawaListId [description]
     * @param  [type] $goodlist   [description]
     * @param  [type] $valuelist  [description]
     * @param  [type] $pricelist  [为-1时则为业务员报价 否则是会计报价]
     * @param  array  $boxlist    [description]
     * @return [type]             [description]
     */
    public static function screate($HawaListId, $goodlist, $valuelist, $pricelist, $idata = [])
    {
        if (empty($HawaListId)) {
            return serr('缺少订单id');
        }
        if (count($goodlist) < count($valuelist)) {
            return serr('订单参数不匹配');
        }
        //避免出现goodid为0的情况出现
        foreach ($goodlist as $key => $value) {
            if (empty($value)) {
                return moerr('货物id出错，请重新尝试。');
            }
        }
        $cdata               = [];
        $ndata['HawaListId'] = $HawaListId;
        if ($pricelist !== -1) {
            if (empty($idata)) {
                foreach ($valuelist as $key => $value) {
                    $ndata['GoodId'] = $goodlist[$key];
                    $ndata['Weight'] = $valuelist[$key];
                    $ndata['Price']  = $pricelist[$key];
                    $cdata[]         = $ndata;
                }
            } else {
                foreach ($valuelist as $key => $value) {
                    $ndata['GoodId']      = $goodlist[$key];
                    $ndata['Weight']      = $valuelist[$key];
                    $ndata['Price']       = $pricelist[$key];
                    $ndata['Box']         = $idata['Box'][$key];
                    $ndata['BoxCount']    = $idata['BoxCount'][$key];
                    $ndata['GoodName']    = $idata['GoodName'][$key];
                    $ndata['ObjectState'] = $idata['ObjectState'][$key];
                    $ndata['Harmful']     = $idata['Harmful'][$key];
                    //关联合同抵扣额度
                    if (!empty($idata['Cont_DedutNum']) && !empty($idata['Cont_DedutNum'][$key])) {
                        $ndata['Cont_DedutNum'] = $idata['Cont_DedutNum'][$key];
                        $ndata['Cont_Id']      = $idata['Cont_Id'][$key];
                    }
                    $cdata[] = $ndata;
                }
            }
        } else {
            if (empty($idata)) {
                foreach ($valuelist as $key => $value) {
                    $ndata['GoodId'] = $goodlist[$key];
                    $ndata['Weight'] = $valuelist[$key];
                    $cdata[]         = $ndata;
                }
            } else {
                foreach ($valuelist as $key => $value) {
                    $ndata['GoodId']      = $goodlist[$key];
                    $ndata['Weight']      = $valuelist[$key];
                    $ndata['Box']         = $idata['Box'][$key];
                    $ndata['BoxCount']    = $idata['BoxCount'][$key];
                    $ndata['GoodName']    = $idata['GoodName'][$key];
                    $ndata['ObjectState'] = $idata['ObjectState'][$key];
                    $ndata['Harmful']     = $idata['Harmful'][$key];
                    //关联合同抵扣额度
                    if (!empty($idata['Cont_DedutNum']) && !empty($idata['Cont_DedutNum'][$key])) {
                        $ndata['Cont_DedutNum'] = $idata['Cont_DedutNum'][$key];
                        $ndata['Cont_Id']      = $idata['Cont_Id'][$key];
                    }
                    $cdata[] = $ndata;
                }
            }
        }
        //产品名完善
        if (!empty($idata['GoodName'])) {
            Goods::NamePerfect($goodlist, $idata['GoodName']);
        }

        //有害成分完善
        if (!empty($idata['Harmful'])) {
            Goods::HarmfulListPerfect($goodlist, $idata['Harmful']);
        }

        $hawa = new Hawa;
        $data = $hawa->saveAll($cdata);
        return sok();
    }

    /**
     * 判断列表中的产品状态是否可操作
     * @param  [type] $list [id数组]
     * @return [type]       [返回错误代码]
     */
    public static function list_state($list)
    {
        $data = Goods::where('State', '<>', Goods::STATEOK)->where('GoodId', 'in', $list)->column('OrderNumber');
        if (!empty($data)) {
            $str = implode("\n", $data);
            $str = "以下危废无法使用：\n" . $str;
            return serr($str);
        } else {
            return sok();
        }
    }
}
